package administrator;

import java.awt.EventQueue;

public class EditQues {

	public JFrame frame;
	private final JLabel lblEditTheQuestion = new JLabel("Edit the Question given below");
	private JTextField QIDfield;
	private JTextPane txtQues;
	private JTextPane txtOptA;
	private JTextPane txtOptB;
	private JTextPane txtOptC;
	private JTextPane txtOptD;
	private JTextPane txtSol;
	private JTextPane txtSolOpt;
	private String s="";
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					EditQues window = new EditQues();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public EditQues() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(350, 35, 550, 720);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBackground(new Color(255, 69, 0));
		panel.setBounds(0, 29, 534, 57);
		frame.getContentPane().add(panel);
		panel.setLayout(null);
		lblEditTheQuestion.setBounds(10, 0, 514, 57);
		panel.add(lblEditTheQuestion);
		lblEditTheQuestion.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblEditTheQuestion.setHorizontalAlignment(SwingConstants.CENTER);
		
		QIDfield = new JTextField();
		QIDfield.setBounds(295, 104, 86, 23);
		frame.getContentPane().add(QIDfield);
		QIDfield.setColumns(10);
		
		JLabel lblEnterTheQid = new JLabel("Enter the QID");
		lblEnterTheQid.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblEnterTheQid.setHorizontalAlignment(SwingConstants.CENTER);
		lblEnterTheQid.setBounds(121, 98, 129, 33);
		frame.getContentPane().add(lblEnterTheQid);
		
		JButton btnOk = new JButton("OK");
		btnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOkActionPerformed(evt);
            }
        });
		btnOk.setBounds(431, 104, 55, 23);
		frame.getContentPane().add(btnOk);
		
		txtQues = new JTextPane();
		txtQues.setBounds(10, 184, 514, 88);
		frame.getContentPane().add(txtQues);
		
		JLabel lblQuestion = new JLabel("Question");
		lblQuestion.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblQuestion.setHorizontalAlignment(SwingConstants.CENTER);
		lblQuestion.setBounds(10, 150, 86, 23);
		frame.getContentPane().add(lblQuestion);
		
		JLabel lblOptionA = new JLabel("Option A");
		lblOptionA.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblOptionA.setHorizontalAlignment(SwingConstants.CENTER);
		lblOptionA.setBounds(10, 277, 86, 23);
		frame.getContentPane().add(lblOptionA);
		
		txtOptA = new JTextPane();
		txtOptA.setBounds(10, 308, 514, 23);
		frame.getContentPane().add(txtOptA);
		
		JLabel lblOptionB = new JLabel("Option B");
		lblOptionB.setHorizontalAlignment(SwingConstants.CENTER);
		lblOptionB.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblOptionB.setBounds(10, 342, 86, 23);
		frame.getContentPane().add(lblOptionB);
		
		txtOptB = new JTextPane();
		txtOptB.setBounds(10, 367, 514, 23);
		frame.getContentPane().add(txtOptB);
		
		JLabel lblOptionC = new JLabel("Option C");
		lblOptionC.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblOptionC.setHorizontalAlignment(SwingConstants.CENTER);
		lblOptionC.setBounds(10, 401, 86, 21);
		frame.getContentPane().add(lblOptionC);
		
		txtOptC = new JTextPane();
		txtOptC.setBounds(10, 423, 514, 23);
		frame.getContentPane().add(txtOptC);
		
		JLabel lblOptionD = new JLabel("Option D");
		lblOptionD.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblOptionD.setHorizontalAlignment(SwingConstants.CENTER);
		lblOptionD.setBounds(10, 457, 86, 23);
		frame.getContentPane().add(lblOptionD);
		
		txtOptD = new JTextPane();
		txtOptD.setBounds(10, 491, 514, 23);
		frame.getContentPane().add(txtOptD);
		
		JLabel lblSolution = new JLabel("Solution");
		lblSolution.setHorizontalAlignment(SwingConstants.CENTER);
		lblSolution.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblSolution.setBounds(10, 525, 86, 23);
		frame.getContentPane().add(lblSolution);
		
		txtSol = new JTextPane();
		txtSol.setBounds(10, 548, 514, 23);
		frame.getContentPane().add(txtSol);
		
		JLabel lblSolutionOption = new JLabel("Solution Option");
		lblSolutionOption.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblSolutionOption.setHorizontalAlignment(SwingConstants.CENTER);
		lblSolutionOption.setBounds(10, 582, 122, 21);
		frame.getContentPane().add(lblSolutionOption);
		
		txtSolOpt = new JTextPane();
		txtSolOpt.setBounds(10, 601, 514, 20);
		frame.getContentPane().add(txtSolOpt);
		
		JButton btnDone = new JButton("Done");
		btnDone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDoneActionPerformed(evt);
            }
        });
		btnDone.setBounds(224, 648, 89, 23);
		frame.getContentPane().add(btnDone);
	}
	
	public String getQues(String s)
	{
		String st1;
		String result1 = "";
		int flag = 0;
		st1= "select * from ques_paper";
		try
        {
            Class.forName("java.sql.Driver");
            
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3307/oats","root","anupam24");
            
            Statement stmt = con.createStatement();
            
            ResultSet rs = stmt.executeQuery(st1);
           
            String qid = "";
            String Ques = "";
            String OptA = "";
            String OptB = "";
            String OptC = "";
            String OptD = "";
            String Sol = "";
            String SolOpt = "";
           
            
            while (rs.next())
            {
            	
            	 qid = rs.getString(1);
            	 Ques = rs.getString(2);
            	 OptA = rs.getString(3);
            	 OptB = rs.getString(4);
            	 OptC = rs.getString(5);
            	 OptD = rs.getString(6);
            	 Sol = rs.getString(7);
            	 SolOpt = rs.getString(8);
            	 
            	 if(qid.equals(s))
            	 {
            		flag = 1;
           			txtQues.setText(Ques);
         			txtOptA.setText(OptA);
         			txtOptB.setText(OptB);
         			txtOptC.setText(OptC);
         			txtOptD.setText(OptD);
         			txtSol.setText(Sol);
         			txtSolOpt.setText(SolOpt);
         			
         			result1 = qid;
         			
         			         			
            	 }
            }
            if(flag == 0)
            {
            	JOptionPane.showMessageDialog(null, " Question does not exists \n" , "Wrong QID", JOptionPane.ERROR_MESSAGE);
            	
            }
            return result1;
            
        }
		catch(Exception e)
        {
            System.out.println("Error is : "+e.getMessage());
            return result1;
        }
		
	}
	
	public int DelQues(String s)
	{
		String str;
		int flag = 0;
		str= "delete from ques_paper where q_id = '"+s+"'" ;	//for delete
		try
        {
            Class.forName("java.sql.Driver");
            
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3307/oats","root","anupam24");
            
            Statement stmt = con.createStatement();
    
            stmt.execute(str);
            stmt.close();
            con.close();
            flag = 1;
            return flag;
        }
        catch(Exception e)
        {
            System.out.println("Error is : "+e.getMessage());
            flag = 0;
            return flag;
        }
	}
	
	public int AddQues(String s)
	{
		int result;
		int counter = 0;
		String QID = s;
		String Question = txtQues.getText();
		String OptA = txtOptA.getText();
		String OptB = txtOptB.getText();
		String OptC = txtOptC.getText();
		String OptD = txtOptD.getText();
		String Ans = txtSol.getText();
		String SolOpt = txtSolOpt.getText();
		
		if( Question == "" || Question.length() == 0)
		{
			JOptionPane.showMessageDialog(null, "Please enter Question \n","Error", JOptionPane.ERROR_MESSAGE);

		}
		else if( Question.length() > 500 )
		{
			JOptionPane.showMessageDialog(null, " Question length can not be more than 500 Digits/Character \n","Error", JOptionPane.ERROR_MESSAGE);						
		}
		else 
		{
			counter = counter + 1;			
		}
		
		
		
		if( OptA == "" || OptA.length() == 0)
		{
			JOptionPane.showMessageDialog(null, "Please enter Option/Choice A \n","Error", JOptionPane.ERROR_MESSAGE);
			counter = counter - 1;
			
		}
		else if( OptA.length() > 100 )
		{
			JOptionPane.showMessageDialog(null, " Length of Option/Choice can not be more than 100 Characters \n","Error", JOptionPane.ERROR_MESSAGE);
			counter = counter - 1;
			
		}
		else
		{
			counter = counter + 1;
		}
		
		
		
		if( OptB == "" || OptB.length() == 0)
		{
			JOptionPane.showMessageDialog(null, "Please enter Option/Choice B \n","Error", JOptionPane.ERROR_MESSAGE);
			counter = counter - 1;
			
		}
		else if( OptB.length() > 100 )
		{
			JOptionPane.showMessageDialog(null, " Length of Option/Choice can not be more than 100 Characters \n","Error", JOptionPane.ERROR_MESSAGE);
			counter = counter - 1;
			
		}
		else 
		{
			counter = counter + 1;
		}
		
		
		if( OptC == "" || OptC.length() == 0)
		{
			JOptionPane.showMessageDialog(null, "Please enter Option/Choice C \n","Error", JOptionPane.ERROR_MESSAGE);
			counter = counter - 1;
			
		}
		else if( OptC.length() > 100 )
		{
			JOptionPane.showMessageDialog(null, " Length of Option/Choice can not be more than 100 Characters \n","Error", JOptionPane.ERROR_MESSAGE);
			counter = counter - 1;
			
		}
		else 
		{
			counter = counter + 1;
		}
		
		
		
		if( OptD == "" || OptD.length() == 0)
		{
			JOptionPane.showMessageDialog(null, "Please enter Option/Choice D \n","Error", JOptionPane.ERROR_MESSAGE);
			counter = counter - 1;
			
		}
		else if( OptD.length() > 100 )
		{
			JOptionPane.showMessageDialog(null, " Length of Option/Choice can not be more than 100 Characters \n","Error", JOptionPane.ERROR_MESSAGE);
			counter = counter - 1;
			
		}
		else 
		{
			counter = counter + 1;
		}
		
		
		
		if( SolOpt == "" || SolOpt.length() == 0)
		{
			JOptionPane.showMessageDialog(null, "Please enter right Option/Choice \n","Error", JOptionPane.ERROR_MESSAGE);
			counter = counter - 1;
			
		}
		else if( SolOpt.length() > 1 )
		{
			JOptionPane.showMessageDialog(null, " Lenth of Solution Option can not be more than 1 Character \n","Error", JOptionPane.ERROR_MESSAGE);
			counter = counter - 1;
			
		}
		else 
		{
			counter = counter + 1;
		}
		
		
		
		if( Ans == "" || Ans.length() == 0)
		{
			JOptionPane.showMessageDialog(null, "Please enter Answer/Solution \n","Error", JOptionPane.ERROR_MESSAGE);
			counter = counter - 1;
			
		}
		else if( Ans.length() > 100 )
		{
			JOptionPane.showMessageDialog(null, " Length of Solution/Answer can not be more than 100 Characters \n","Error", JOptionPane.ERROR_MESSAGE);
			counter = counter - 1;
			
		}
		else 
		{
			counter = counter + 1;
		}
		
		
				
		result = counter;
		
		
		try
        {
			if(counter == 7)
			{
				Class.forName("java.sql.Driver");
	            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3307/oats","root","anupam24");
	            Statement stmt = con.createStatement();
	
	            String sql="";
	            sql = "INSERT INTO ques_paper VALUES ( '"+QID+"','"+Question+"','"+OptA+"'";
	            sql = sql + ",'"+OptB+"','"+OptC+"','"+OptD+"'";
	            sql = sql + ",'"+Ans+ "','"+SolOpt + "');";
	               
	            stmt.execute(sql);
	            stmt.close();
	            con.close();
	            JOptionPane.showMessageDialog(null," Question Updated Successfully " ,"Question Edited", JOptionPane.PLAIN_MESSAGE);
			}
			else
			{
				JOptionPane.showMessageDialog(null," Please Enter all the details " ,"Error", JOptionPane.ERROR_MESSAGE);
			}
            
        }
        catch(Exception e)
        {
            System.out.println("Error is : "+e.getMessage());
            JOptionPane.showMessageDialog(null, e.getMessage() ,"Error", JOptionPane.ERROR_MESSAGE);
            
        }
        return result;
	}
	
	
	private void btnOkActionPerformed(java.awt.event.ActionEvent evt) 
	{
		s = QIDfield.getText();
		String str = "";
		str = getQues(s);
		
		System.out.println(str);
	}
	
	private void btnDoneActionPerformed(java.awt.event.ActionEvent evt)
	{
		
		DelQues(s);
		int flag = AddQues(s);
		if(flag == 7)
		{
			QuesSetUp qs = new QuesSetUp();
			qs.frame.setVisible(true);
			this.frame.setVisible(false);
		}
		
	}
	
}
